package top.drinyeit.weilong.presenter;

import android.content.Intent;

import java.math.BigDecimal;

import top.drinyeit.weilong.activity.PaySuccessActivity;
import top.drinyeit.weilong.activity.QccodePayActivity;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;
import top.drinyeit.weilong.util.SPUtil;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by Administrator on 2018/1/30.
 */

public class QccodePayPresenter {
    private QccodePayActivity activity;
    public QccodePayPresenter(QccodePayActivity activity){
        this.activity = activity;
    }

    //线下扫码支付
    public void scanPayment(final BigDecimal payment_amount, String store_str){
        String user_id = SPUtil.getShareString(Constant.USER_ID);
        if (user_id.equals("")){
            activity.showToastMsg("请先登录");
            return;
        }
        activity.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                activity.dismissLoading();
                activity.showToastMsg(o.getMessage());
                activity.showToastMsg(o.getMessage());
                if (o.getCode() == COMMON_SUCCESS){
                    Intent intent = new Intent(activity, PaySuccessActivity.class);
                    intent.putExtra("store_name",activity.qccodePayBean.getName());
                    intent.putExtra("money",payment_amount.toString());
                    activity.startActivity(intent);
                    activity.finish();

                }
            }
        });
        RetrofitMethod.getInstance().scanPayment(subscriber,user_id,payment_amount,store_str);
    }
}
